package com.yl.auth.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yl.auth.entity.Danwei;
import com.yl.auth.entity.Gongyingshang;
import com.yl.auth.entity.Shicai;
import com.yl.auth.model.vo.ShicaiVo;
import com.yl.auth.service.DanweiService;
import com.yl.auth.service.GongyingshangService;
import com.yl.auth.service.ShicaiService;
import com.yl.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.xml.bind.ValidationEvent;
import java.time.temporal.ValueRange;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author L
 * @since 2023-07-07
 */
@RestController
@RequestMapping("/admin/auth/shicai")
@Api(tags = "机构中心端-膳食管理")
public class ShicaiController {
    @Resource
    private ShicaiService shicaiService;
    @Resource
    private GongyingshangService gongyingshangService;
    @Resource
    private DanweiService danweiService;

    /**
     * 食材管理分页查询
     */
    @ApiOperation(value = "食材管理")
    @GetMapping("/getShicai/{page}/{limit}")
    public Result getShicai(@PathVariable @ApiParam("页码") Long page,
                            @PathVariable @ApiParam("记录") Long limit){
        Page<ShicaiVo> page1=new Page<>(page,limit);
        Page<ShicaiVo> list=shicaiService.getShicai(page1);
        return Result.ok(list);
    }

    /**
     * 新增食材
     */
    @ApiOperation(value = "新增食材")
    @PostMapping("/save")
    public Result save(@RequestBody Shicai shicai){
        shicaiService.save(shicai);
        return Result.ok();
    }

    /**
     * 查询单位
     */
    @ApiOperation(value = "查询单位")
    @GetMapping("/getDanwei")
    public Result getDanwei(){
        List<Danwei> dlist=danweiService.list();
        return Result.ok(dlist);
    }

    /**
     * 查询供应商
     */
    @ApiOperation(value = "查询供应商")
    @GetMapping("/getGys")
    public Result getGys(){
        List<Gongyingshang> glist=gongyingshangService.getGys();
        return Result.ok(glist);
    }

    /**
     * 删除食材
     */
    @ApiOperation(value = "删除食材")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable @ApiParam("食材id") Integer id){
        shicaiService.removeById(id);
        return Result.ok();
    }

    /**
     * 根据id查询
     */
    @ApiOperation(value = "回显食材")
    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable @ApiParam("食材id") Long id){
        Shicai shicai=shicaiService.getByIds(id);
        return Result.ok(shicai);
    }

    /**
     * 价格更新（修改）
     */
    @ApiOperation(value = "价格更新")
    @PutMapping("/update")
    public Result update(@RequestBody Shicai shicai){
        QueryWrapper<Shicai> shicaiQueryWrapper=new QueryWrapper<>();
        shicaiQueryWrapper.eq("id",shicai.getId());
        List<Shicai> list = shicaiService.list(shicaiQueryWrapper);
        Shicai shicai1 = list.get(list.size() - 1);
        shicai1.setStatus("1");
        shicaiService.save(shicai1);
        shicaiService.updateById(shicai);
        return Result.ok();
    }

    /**
     * 根据id查询食才供应商（价格分析）
     */
    @ApiOperation(value = "根据id价格分析")
    @GetMapping("/getByIdJiage/{id}")
    public Result getByIdJiage(@PathVariable @ApiParam("食材id") Long id){
        ShicaiVo shicaiVo=shicaiService.getByIdJiage(id);
        return Result.ok(shicaiVo);
    }

    /**
     * 供应商管理
     */
    @ApiOperation(value = "供应商管理")
    @GetMapping("/getGongYingShang/{page}/{limit}")
    public Result getGongYingShang(@PathVariable @ApiParam("页码") Long page,
                                   @PathVariable @ApiParam("记录") Long limit){
        Page<Gongyingshang> page1=new Page<>(page,limit);
        Page<Gongyingshang> list=gongyingshangService.getGongYingShang(page1);
        return Result.ok(list);
    }

    /**
     * 新增供应商
     */
    @ApiOperation(value = "新增供应商")
    @PostMapping("/addGys")
    public Result addGys(@RequestBody Gongyingshang gongyingshang){
        gongyingshangService.save(gongyingshang);
        return Result.ok();
    }

    /**
     * 删除供应商
     */
    @ApiOperation(value = "删除供应商")
    @DeleteMapping("/delGys/{id}")
    public Result delGys(@PathVariable @ApiParam("供应商id") Long id){
        gongyingshangService.removeById(id);
        return Result.ok();
    }

    /**
     * 根据id回显供应商
     */
    @ApiOperation(value = "回显供应商")
    @GetMapping("/getGysById/{id}")
    public Result getGysById(@PathVariable @ApiParam("供应商id") Long id){
        Gongyingshang gongyingshang=gongyingshangService.getById(id);
        return Result.ok(gongyingshang);
    }

    /**
     * 编辑供应商
     */
    @ApiOperation(value = "编辑供应商")
    @PutMapping("/updateGys")
    public Result updateGys(@RequestBody Gongyingshang gongyingshang){
        gongyingshangService.updateById(gongyingshang);
        return Result.ok();
    }

    /**
     * 根据供应商的id查询所供应的食材
     */
    @ApiOperation(value = "供应食材")
    @GetMapping("/getGysc/{id}")
    public Result getGysc(@PathVariable @ApiParam("供应商id") Long id){
        List<ShicaiVo> list=shicaiService.getGysc(id);
        return Result.ok(list);
    }

    /**
     * 调用修改之前的数据
     */
    @ApiOperation(value = "回调")
    @PostMapping("/getHuiDao")
    public Result getHuiDao(@RequestBody ShicaiVo shicaiVo){
        List<ShicaiVo> list=shicaiService.getHuiDao(shicaiVo);
        return Result.ok(list);
    }
}

